import http from '@/common/http.js'


// 1. 数据进行处理
export const requestDataSortManager = (url, data) => {
	var clientid = uni.getStorageSync('client_id');
	var access_token = uni.getStorageSync('access_token');

	var newData = {}
	if (data) {
		newData = data
	}
	

	return newData
}

// 2. 返回数据进行处理
export const responseDataSortManager = (url, res) => {
	var tempData = {}
	
	
	var backKeys = Object.keys(res);
	if (backKeys.indexOf('rows') !== -1){
		var data = {
			total:res.total,
			list :res.rows
		}
		res.data = data;
	} 
		
	if (res.data && res.data !== 0) {
		tempData = res.data
	} else if (res.body && res.body !== 0) {
		tempData = res.body
	}

	var responseInfo = {
		data: tempData,
		success: res.code === 200 ? true : (res.success === true ? true : false),
		errMsg: res.msg ? res.msg : (res.errMsg ? res.errMsg : '未知异常')
	}

	if (res.code === 401) {
		// uni.showToast({
		// 	title: '您好，每日需重新登陆',
		// 	duration: 2000
		// })

		// // 1. 清空用户code
		// uni.removeStorageSync('code')
		// // 2. 清空用户token
		// uni.removeStorageSync('openid')
		// // 3. 清空sessionKey
		// uni.removeStorageSync('session_key')
		// // 4. 清空token
		// uni.removeStorageSync('auth')
		// // 5. 清空memberID
		// uni.removeStorageSync('memberId')

		// // 4. 清空当前用户信息
		// getApp().globalData.userInfo = null

		// // 1. 获取当前页面的实例
		// var pages = getCurrentPages()
		// // 2. 获取上一页
		// var prevPage = pages[0]
		// console.log(prevPage)
		// // 3. 调用上个页面的方法 // 通知mine 页面进行刷新数据
		// prevPage.$vm.logoutSuccessManager(0)

		return
	}


	return responseInfo
}



export const GET = (url, data) => {
	return new Promise(resolve => {
		var newData = requestDataSortManager(url, data)
		http.request({
			url,
			method: 'get',
			params: newData
		}).then(res => {
			
			var response = responseDataSortManager(url, res)
			resolve(response)
		})
	})
}

export const POST = (url, data, responseType) => {
	var newData = requestDataSortManager(url, data)

	return new Promise(resolve => {
		var newData = requestDataSortManager(url, data)
		http.request({
			url: url,
			method: 'post',
			params: newData,
			responseType: responseType
		}).then(res => {
			var response = responseDataSortManager(url, res)
			resolve(response)
		})
	})
}

export const POSTJSON = (url, data) => {
	var newData = requestDataSortManager(url, data)

	return new Promise(resolve => {
		var newData = requestDataSortManager(url, data)
		http.request({
			url,
			method: 'post',
			params: newData,
			header: {
				'Content-type': 'application/json'
			},
		}).then(res => {
			console.log(res)
			var response = responseDataSortManager(url, res)
			resolve(response)
		})
	})
}

export const POSTFORM = (url, data) => {
	var newData = requestDataSortManager(url, data)

	return new Promise(resolve => {
		var newData = requestDataSortManager(url, data)
		http.request({
			url: url,
			method: 'post',
			params: newData,
			header: {
				'Content-type': 'application/x-www-form-urlencoded'
			}
		}).then(res => {
			var response = responseDataSortManager(url, res)
			resolve(response)
		})
	})
}

export const PUT = (url, data) => {
	var newData = requestDataSortManager(url, data)

	return new Promise(resolve => {
		var newData = requestDataSortManager(url, data)
		http.request({
			url: url,
			method: 'put',
			params: newData,
			header: {
				'Content-type': 'application/json'
			}
		}).then(res => {
			var response = responseDataSortManager(url, res)
			resolve(response)
		})
	})
}

export const DELETE = (url, data) => {
	return http.request({
		url,
		method: 'delete',
		params: data
	})
}

export const NoLogin = (url, data) => {
	return http.noLogin({
		url,
		method: 'post',
		params: data
	})
}



// post 但是报错不影响主线程
export const POSTJSONNOBACK = (url, data) => {
	var newData = requestDataSortManager(url, data)

	return new Promise(resolve => {
		var newData = requestDataSortManager(url, data)
		http.request({
			url,
			method: 'post',
			params: newData,
			header: {
				'Content-type': 'application/json'
			}
		}).then(res => {

		})
	})
}


// 请求微信
export const POSTWITHWECHAT = (url, data) => {
	return new Promise(resolve => {
		uni.request({
			url: url,
			success(res) {
				resolve(res);
			},
			fail: function(err) {
				uni.showToast({
					icon: 'none',
					title: '请检查您的网络~~'
				});
			}
		})
	});
}